# @Time:2021/6/21 14:42
# @Author:5120195554王欣霏
# -*- coding = utf-8 -*-

import os
import pygame
from pygame.constants import QUIT
from scene.tiled_scene import TileScene
from actor.swk import SWK
import pytmx
from pygame.locals import *
from scene.fade_scene import FadeScene
from scene import SceneResult
from dialog.battle_dialog1 import BattleDialog1
from scene.fade_scene import SceneStatus
from actor.bear import Bear
from actor.bear_battle import BearBattleStatus


class MountainScene(object):
    scene_result = SceneResult.Ongoing

    def __init__(self, swk: SWK, surface: pygame.Surface):
        self.screen = surface
        self.swk_player = swk
        self.swk_pos_x = 0
        self.swk_pos_y = 0
        backtmxpath = os.path.join('../resource', 'tmx', 'bear.tmx')
        self.tile_scene = TileScene(backtmxpath)
        self.back_surface = self.tile_scene.surface
        self.fade_scene = FadeScene(self.back_surface)
        self.temp_surface = pygame.Surface((800, 600))
        self.obstacle_group = pygame.sprite.Group()
        # self.bear = pygame.sprite.Sprite()
        self.bear = pygame.sprite.Group()

        self.win_pos_x = 0
        self.win_pos_y = 0
        self.dx_dy = [0, 0]
        self.init_actor()
        self.battle_dialog = None

    def init_actor(self):
        for group in self.tile_scene.tiled.tmx_data.objectgroups:
            if isinstance(group, pytmx.pytmx.TiledObjectGroup):
                if group.name == 'actor':
                    for obj in group:
                        if obj.name == 'sun':
                            self.swk_player.set_pos(obj.x, obj.y)
                            self.win_pos_x = obj.x - 400
                            self.win_pos_y = obj.y - 300
                if group.name == 'obstacle':
                    for obj in group:
                        obs = pygame.sprite.Sprite()
                        obs.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.obstacle_group.add(obs)
                if group.name == 'monster':
                    for obj in group:
                        bear = Bear(obj.x, obj.y)
                        self.bear.add(bear)

    def get_curr_surface(self):
        self.win_surface = self.fade_scene.get_back_image(self.win_pos_x, self.win_pos_y)
        self.temp_surface.blit(self.win_surface, (0, 0))
        self.swk_player.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)
        # self.bear.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)
        for bear in self.bear:
            bear.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)

        if self.battle_dialog and not self.battle_dialog.over_show:
            self.battle_dialog.draw(self.temp_surface)

        self.screen.blit(self.temp_surface, (0, 0))

    def run(self):
        clock = pygame.time.Clock()
        scene_exit = False
        while not scene_exit:
            key_down = False
            self.dx_dy = [0, 0]
            # self.bear.Move()
            for bear in self.bear:
                bear.draw(self.temp_surface,self.win_pos_x,self.win_pos_y)

            for event in pygame.event.get():
                if event.type == QUIT:
                    self.scene_result = SceneResult.Quit
                    scene_exit = True
                if event.type == KEYDOWN:
                    key_down = True
                    pressed_key = event.key
                    self.dx_dy = self.swk_player.key_move(pressed_key, key_down, self.obstacle_group)
                    if key_down == True:
                        self.win_pos_x += self.dx_dy[0]
                        self.win_pos_y += self.dx_dy[1]

            # 越界判断
            if self.win_pos_x < 0:
                self.win_pos_x = 0
            if self.win_pos_y < 0:
                self.win_pos_y = 0
            if self.win_pos_x > (1999 - 800):
                self.win_pos_x = 1999 - 800
            if self.win_pos_y > (1495 - 600):
                self.win_pos_y = 1495 - 600

            self.create_battle_dialog()
            if self.battle_dialog and not self.battle_dialog.over_show:
                self.battle_dialog.process(key_down, pressed_key)
                self.swk_player.hp = self.battle_dialog.swk.hp
                self.bear_status = self.battle_dialog.monster.status
                # print(self.bear_status)
                if self.bear_status == BearBattleStatus.DieOver:
                    self.scene_result = SceneResult.Win
                    scene_exit = True

            self.get_curr_surface()
            pygame.display.update()
            clock.tick(15)

    def create_battle_dialog(self):
        collide_flag = False
        if self.battle_dialog is None:
            collide_flag = True
        elif self.battle_dialog.over_show:
            collide_flag = True
        if collide_flag:
            collide_result = pygame.sprite.spritecollide(self.swk_player, self.bear,True)
            if collide_result:
                self.battle_dialog = BattleDialog1(self.swk_player.hp)
